import config from "../../utils/config";
import {
	getOnlineRetailerID
} from "../../utils/api";

let state = {
	orUuid: "",
	cartInfo: uni.getStorageSync("cartInfo") || [],
	address_data: uni.getStorageSync("address_data") || {},
};

let getters = {
	orUuid: (state) => state.orUuid,
	cartInfo: (state) => state.cartInfo,
	address_data: (state) => state.address_data,
};

let mutations = {
	updateState(state, payload) {
		for (const key in payload) {
			if (Object.hasOwnProperty.call(payload, key)) {
				state[key] = payload[key];
				uni.setStorageSync(key, payload[key]);
			}
		}
	},
};

let actions = {
	async loadOnlineRetailerID({
		state
	}, payload) {
		state.orUuid = await getOnlineRetailerID(config.appid);
	},

	save_cart(context, payload) {
		context.commit("updateState", payload);
	},

	save_address(context, payload) {
		context.commit("updateState", payload);
	},
};

export default {
	namespaced: true,
	state,
	getters,
	mutations,
	actions,
};
